﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace TestAppStart
{
    class Program
    {
        static void Main(string[] args)
        {
            WorkControl();

        }
        static int colorId = 0;
        static readonly ConsoleColor[] consoleColors = { ConsoleColor.Red, ConsoleColor.Yellow, ConsoleColor.White, ConsoleColor.Blue, ConsoleColor.Green };
        private static void SetColor()
        {
            if (colorId > consoleColors.Length)
                colorId = 0;

            Console.ForegroundColor = consoleColors[colorId % consoleColors.Length];
            colorId++;
        }
        private static void WorkControl()
        {
            SetColor();
            Console.WriteLine("0: 退出程序");
            SetColor();
            Console.WriteLine("1: 启动程序");
            SetColor();
            Console.WriteLine("2: 停止程序");
            SetColor();
            var key = Console.ReadKey();
            switch (key.Key)
            {
                case ConsoleKey.D0:
                case ConsoleKey.NumPad0:
                    return;
                case ConsoleKey.D1:
                case ConsoleKey.NumPad1:
                    StartProcess();
                    break;
                case ConsoleKey.D2:
                case ConsoleKey.NumPad2:
                    StopProcess();
                    break;
                default:
                    break;
            }
            WorkControl();
        }
        private static Process appProcess;
        private static void StartProcess()
        {
            try
            {
                Process p = null;
                try
                {
                    if (appProcess != null)
                        p = Process.GetProcessById(appProcess.Id);
                }
                catch { }
                if (p != null && !p.HasExited && p.ProcessName == appProcess.ProcessName)
                {
                    Console.WriteLine("程序运行中");
                }
                else
                {
                    string fileName = @"D:\Csmp-Boot\nginx-1.17.6\nginx.exe";
                    ProcessStartInfo startInfo = new ProcessStartInfo()
                    {
                        FileName = fileName, Arguments="",
                        WorkingDirectory = Path.GetDirectoryName(fileName), CreateNoWindow=true, UseShellExecute= true
                    };
                    appProcess = Process.Start(startInfo);
                    Console.WriteLine("程序启动");
                }
            }
            catch (Exception ex)
            {

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
        private static void StopProcess()
        {

            try
            {
                var p = Process.GetProcessById(appProcess.Id);
                if (p != null && !p.HasExited && p.ProcessName == appProcess.ProcessName)
                    appProcess.Kill();
                else
                    Console.WriteLine("程序未运行");

                Console.WriteLine("程序停止");
            }
            catch (Exception ex)
            {

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
    }
}
